package com.eshop.modules.canvas.rest;

import java.lang.invoke.SerializedLambda;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import java.util.Arrays;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import java.util.Map;
import com.eshop.tools.domain.QiniuContent;
import com.eshop.tools.service.dto.LocalStorageDto;
import java.util.HashMap;
import com.eshop.api.EshopException;
import com.eshop.enums.ShopCommonEnum;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestParam;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.canvas.domain.StoreCanvas;
import com.eshop.utils.RedisUtils;
import com.eshop.tools.service.QiNiuService;
import com.eshop.tools.service.LocalStorageService;
import com.eshop.modules.canvas.service.StoreCanvasService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "画布管理" })
@RestController
@RequestMapping({ "/api/canvas" })
public class StoreCanvasController
{
    private final StoreCanvasService storeCanvasService;
    private final LocalStorageService localStorageService;
    private final QiNiuService qiNiuService;
    private final RedisUtils redisUtils;
    
    @PostMapping({ "/saveCanvas" })
    @Log("新增或修改画布")
    @ApiOperation("新增或修改画布")
    public ResponseEntity<Object> create(@Validated @RequestBody final StoreCanvas resources) {
        return new ResponseEntity(this.storeCanvasService.saveOrUpdate(resources), HttpStatus.CREATED);
    }
    
    @ApiOperation("上传文件")
    @PostMapping({ "/upload" })
    public ResponseEntity<Object> create(@RequestParam(defaultValue = "") final String name, @RequestParam(defaultValue = "") final String type, @RequestParam("file") final MultipartFile file) {
        String localUrl = this.redisUtils.getY("admin_api_url");
        if (StrUtil.isBlank((CharSequence)type)) {
            localUrl = this.redisUtils.getY("api_url") + "/api";
        }
        final String mode = this.redisUtils.getY("file_store_mode");
        StringBuilder url = new StringBuilder();
        if (ShopCommonEnum.STORE_MODE_1.getValue().toString().equals(mode)) {
            if (StrUtil.isBlank((CharSequence)localUrl)) {
                throw new EshopException("本地上传,请先登陆系统配置后台/移动端API地址");
            }
            final LocalStorageDto localStorageDTO = this.localStorageService.create(name, file);
            if ("".equals(url.toString())) {
                url = url.append(localUrl + "/file/" + localStorageDTO.getType() + "/" + localStorageDTO.getRealName());
            }
            else {
                url = url.append("," + localUrl + "/file/" + localStorageDTO.getType() + "/" + localStorageDTO.getRealName());
            }
        }
        else {
            final QiniuContent qiniuContent = this.qiNiuService.upload(file, this.qiNiuService.find());
            if ("".equals(url.toString())) {
                url = url.append(qiniuContent.getUrl());
            }
            else {
                url = url.append("," + qiniuContent.getUrl());
            }
        }
        final Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("errno", 0);
        map.put("link", url);
        return new ResponseEntity(map, HttpStatus.CREATED);
    }
    
    @GetMapping({ "/getCanvas" })
    @ApiOperation("读取画布数据")
    public ResponseEntity<StoreCanvas> getCanvas(final StoreCanvas storeCanvas) {
        final StoreCanvas canvas = (StoreCanvas)((LambdaQueryChainWrapper)this.storeCanvasService.lambdaQuery().eq(StoreCanvas::getTerminal, storeCanvas.getTerminal())).one();
        return (ResponseEntity<StoreCanvas>)new ResponseEntity(canvas, HttpStatus.OK);
    }
    
    @Log("删除画布")
    @ApiOperation("删除画布")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        Arrays.asList(ids).forEach(id -> this.storeCanvasService.removeById((Serializable)id));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public StoreCanvasController(final StoreCanvasService storeCanvasService, final LocalStorageService localStorageService, final QiNiuService qiNiuService, final RedisUtils redisUtils) {
        this.storeCanvasService = storeCanvasService;
        this.localStorageService = localStorageService;
        this.qiNiuService = qiNiuService;
        this.redisUtils = redisUtils;
    }
}
